package eclipse.util.maptools;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
import eclipse.ErrorManager;
import eclipse.lib.graphics.GLTextureLoader;
import eclipse.util.*;
/**
 * The TileSheetDisplay class displays a tilesheet
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class TilesheetDisplay extends JPanel implements MouseListener
{
	private static final long serialVersionUID = 1L;
	//Image of the background
	private Image img;
	//Image of the box to select a section of the tilesheet
	private final Image box;	
	/**
	 * Constructor to the TileheetDisplay class. 
	 * This just loads the box Object and the mouse listener 
	 */
	public TilesheetDisplay()
	{
		//load the image of the box
		box = GLTextureLoader.makeImage(new ERL(MapEditorApplication.workspace + "box").openStream());
		//add a mouse listener to the panel
		addMouseListener(this);		
	}
	//redraws the panel
	public final void paintComponent(Graphics g) 
	{
		try
		{
			//draw the tilesheet
			g.drawImage(img, 0, 0, null);
			//draw the select-box
			g.drawImage(
					box,
					MapEditorApplication.selectX,
					MapEditorApplication.selectY,
					null);
		}
		catch(Exception e){}
	}
	/**
	 * Sets the background image to the Image specified.
	 * @param img An Image file that the JPanel's background will be set to.
	 */
	public final void setImage(Image img)
	{
		this.img = img;
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);		    		    	
	}
	/**
	 * Sets the background image to the Image specified.
	 * @param sImage A path to the Image file represented with a String.
	 */
	public void setImage(String sImage)
	{
		setImage(new ImageIcon(sImage).getImage());
	}
	/**
	 * Sets the background image to the Image specified.		 
	 * @param iImage An InputStream representing the Image file.
	 */
	public void setImage(InputStream iImage)
	{
		try
		{
			setImage(ImageIO.read(iImage));
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Sets the background image to the Image specified.
	 * @param uImage A path to the Image file represented as a URL.
	 */
	public void setImage(URL uImage)
	{
		setImage(new ImageIcon(uImage).getImage());
	}
	@Override
	public final void mouseClicked(MouseEvent e) 
	{
		int pX = e.getX();
		int pY = e.getY();
		
		MapEditorApplication.selectX = (pX / 32) * 32;		
		MapEditorApplication.selectY = (pY / 32) * 32;		
		repaint();
	}	
	public final void mouseEntered(MouseEvent e)	{}
	public final void mouseExited(MouseEvent e) 	{}
	public final void mousePressed(MouseEvent e) 	{}
	public final void mouseReleased(MouseEvent e) {}
}
